package us.icitap.cm.ui.shared;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.entities.Officer;
import us.icitap.cm.business.entities.UniqueNumber;
import us.icitap.cm.listeners.ReturnObjectListener;
import us.icitap.cm.ui.custom.CustSearchButton;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Runo;

public class OfficerField extends CustomField<Officer>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6236793437661733013L;

	private final CustSearchButton searchButton = new CustSearchButton();
	
	private SearchOfficerWindow searchOfficerWindow = new SearchOfficerWindow();
	
	
	private final  CustomLayout customLayout = new CustomLayout("officerLayout");
	
	public OfficerField() {
		
	}
	
	public OfficerField(String caption) {
	
		this.setCaption(caption);
	}
	

	
	@Override
	protected Component initContent() {
	
		customLayout.setWidth("500px");
		customLayout.addComponent(searchButton, "search");
		
		searchButton.addClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 3789887969270761323L;

			@Override
			public void buttonClick(ClickEvent event) {
				if(searchOfficerWindow.getParent()==null)
					CaseManagementUI.getCurrent().addWindow(searchOfficerWindow);				
			}
		});
		
		searchButton.setStyleName(Runo.BUTTON_LINK);
		searchButton.setIcon(new ThemeResource("img/icons/blue/Search.png"));
		//searchButton.setSizeUndefined();
		this.searchOfficerWindow.addReturnObjectListener(new ReturnObjectListener() {
			
			@Override
			public <T> void returnObject(T result) {
				Officer officer = (Officer) result;
				setValue(officer);
				
				
			}
		});
		return customLayout;
	}

	@Override
	public Class<? extends Officer> getType() {
		return Officer.class;
	}

	public void showOfficer(Officer officer){

		Label lOfficer = new Label(officer != null? (officer.getPoliceId()+", "+officer.getNameSurname()):"");
		Label lOrgStructure = new Label((officer != null && officer.getOrgStructure() != null)?officer.getOrgStructure().getName():"");
		customLayout.removeComponent("officer");
		customLayout.removeComponent("orgStructure");
		customLayout.addComponent(lOfficer,"officer");
		customLayout.addComponent(lOrgStructure, "orgStructure");
		
	}
	
	@Override
	protected void setInternalValue(Officer newValue) {
		super.setInternalValue(newValue);
		showOfficer(newValue);
	}

	@Override
	public void setEnabled(boolean enabled) {
		searchButton.setVisible(enabled);
	}
	
	
	
}
